package com.example.ice.fittime.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.ice.fittime.R;
import com.example.ice.fittime.adapter.HomeRecyclerAdapter;
import com.example.ice.fittime.entry.ArticleBanner;
import com.example.ice.fittime.entry.ExperienceShare;
import com.example.ice.fittime.entry.Topic;
import com.example.ice.fittime.entry.UserActivity;
import com.example.ice.fittime.global.ClassType;
import com.example.ice.fittime.util.NetUtil;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment implements Callback<List<ArticleBanner>>,Handler.Callback {


    private RecyclerView home_recyclerView;
    private LinearLayoutManager layoutManager;
    private HomeRecyclerAdapter homeRecyclerAdapter;
    private Handler handler = new Handler(this);
    private List<ArticleBanner> bannerList;
    private List<ExperienceShare> shareList;
    private List<UserActivity> userActivityList;
    private List<Topic> topicList;
    private List<Object> dataList;
    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_home, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        home_recyclerView = ((RecyclerView) view.findViewById(R.id.home_recyclerView));
        bannerList = new ArrayList<>();
        shareList = new ArrayList<>();
        userActivityList = new ArrayList<>();
        topicList = new ArrayList<>();
        dataList = new ArrayList<>();
        layoutManager = new LinearLayoutManager(getContext());
        home_recyclerView.setLayoutManager(layoutManager);
        homeRecyclerAdapter = new HomeRecyclerAdapter(new ArrayList<>(),getContext());
        home_recyclerView.setAdapter(homeRecyclerAdapter);
        NetUtil.getData(ClassType.ArticleBanner,this);
        getExperienceShare();
        getUserActivity();
        getTopic();
    }

    private void getTopic() {
        NetUtil.getData(ClassType.Topic, new Callback<List<Topic>>() {
            @Override
            public void onResponse(Call<List<Topic>> call, Response<List<Topic>> response) {
                topicList = response.body();
                if (userActivityList.size() != 0 && bannerList.size() != 0) {
                    dataList.add(2,topicList);
                    if (shareList.size() != 0) {
                        dataList.addAll(shareList);
                        handler.sendEmptyMessage(1);
                    }
                }
            }

            @Override
            public void onFailure(Call<List<Topic>> call, Throwable t) {
                getTopic();
                Toast.makeText(getContext(), "数据请求失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void getUserActivity() {
        NetUtil.getData(ClassType.UserActivity, new Callback<List<UserActivity>>() {
            @Override
            public void onResponse(Call<List<UserActivity>> call, Response<List<UserActivity>> response) {
                userActivityList = response.body();
                if (bannerList.size() != 0) {
                    dataList.add(1,userActivityList);
                    if (topicList.size() != 0) {
                        dataList.add(2,topicList);
                        if (shareList.size() != 0) {
                            dataList.addAll(shareList);
                            handler.sendEmptyMessage(1);
                        }
                    }
                }

            }

            @Override
            public void onFailure(Call<List<UserActivity>> call, Throwable t) {
                Toast.makeText(getContext(), "数据请求失败", Toast.LENGTH_SHORT).show();
                getUserActivity();
            }
        },System.currentTimeMillis()+"");
    }

    private void getExperienceShare() {
        NetUtil.getData(ClassType.ExperienceShare, new Callback<List<ExperienceShare>>() {
            @Override
            public void onResponse(Call<List<ExperienceShare>> call, Response<List<ExperienceShare>> response) {
                shareList =  response.body();
                if (bannerList.size()!=0 && topicList.size()!=0 && userActivityList.size()!=0) {
                    dataList.addAll(shareList);
                    handler.sendEmptyMessage(1);
                }
            }

            @Override
            public void onFailure(Call<List<ExperienceShare>> call, Throwable t) {
                Toast.makeText(getContext(), "数据请求失败", Toast.LENGTH_SHORT).show();
                getExperienceShare();
            }
        },System.currentTimeMillis()+"","false");
    }

    @Override
    public void onResponse(Call<List<ArticleBanner>> call, Response<List<ArticleBanner>> response) {
        bannerList = response.body();
        dataList.add(0,bannerList);
        if (userActivityList.size() != 0) {
            dataList.add(1,userActivityList);
            if (topicList.size() != 0 ) {
                dataList.add(2,topicList);
                if (shareList.size() != 0) {
                    dataList.addAll(shareList);
                    handler.sendEmptyMessage(1);
                }
            }
        }
    }

    @Override
    public void onFailure(Call<List<ArticleBanner>> call, Throwable t) {
        Toast.makeText(getContext(), "数据请求失败", Toast.LENGTH_SHORT).show();
        NetUtil.getData(ClassType.ArticleBanner,this);
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case 1:
                homeRecyclerAdapter.addAll(dataList);
                break;
        }
        return true;
    }
}
